﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ocmf.Types;

namespace Ocmf.Forms
{
    public partial class AssignExperimentForm : Form
    {
        public AssignExperimentForm()
        {
            InitializeComponent();

            DatabaseProvider.RefreshChambers();
            DatabaseProvider.SynchronizeExperimentFolder();

            //fill chambers checklistbox
            foreach (var key in DatabaseProvider.Chambers.Keys)
                chambersCheckedListBox.Items.Add(key);
            //fill experiments combobox
            foreach (var experiment in DatabaseProvider.Experiments.Keys)
                experimentsComboBox.Items.Add(experiment);
            if (experimentsComboBox.Items.Count > 0)
                experimentsComboBox.SelectedIndex = 0;
            //refresh datagrid view for previous assigned experiments
            refreshAssignedChambersDataGridView();
        }

        private void assignButton_Click(object sender, EventArgs e)
        {
            //set the running experiment value for each checked chamber
            foreach(string chamber in chambersCheckedListBox.CheckedItems)
                DatabaseProvider.Chambers[chamber].RunningExperiment = DatabaseProvider.Experiments[experimentsComboBox.SelectedItem.ToString()];

            refreshAssignedChambersDataGridView();
        }

        private void experimentsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //reset the checked list
            refreshChambersCheckedListBox();
        }

        private void assignedChambersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            assignedChambersDataGridView.ClearSelection();
            assignedChambersDataGridView.Rows[e.RowIndex].Selected = true;
        }

        private void clearSelectedButton_Click(object sender, EventArgs e)
        {
            //null the running experiment property for all selected chambers
            foreach (DataGridViewRow row in assignedChambersDataGridView.SelectedRows)
                DatabaseProvider.Chambers[row.Cells["chamberAliasColumn"].Value.ToString()].RunningExperiment = null;

            refreshAssignedChambersDataGridView();
            refreshChambersCheckedListBox();
        }

        private void clearAllButton_Click(object sender, EventArgs e)
        {
            //null the running experiment property for all chambers
            foreach (DataGridViewRow row in assignedChambersDataGridView.Rows)
                DatabaseProvider.Chambers[row.Cells["chamberAliasColumn"].Value.ToString()].RunningExperiment = null;

            refreshAssignedChambersDataGridView();
            refreshChambersCheckedListBox();
        }

        private void refreshAssignedChambersDataGridView()
        {
            //clear any past values displayed
            assignedChambersDataGridView.Rows.Clear();

            //add any assigned experiments to the gridview
            foreach (var chamber in DatabaseProvider.Chambers)
                if (chamber.Value.RunningExperiment != null)
                    assignedChambersDataGridView.Rows.Add(new object[] { chamber.Key, chamber.Value.RunningExperiment.FileName });
        }

        private void refreshChambersCheckedListBox()
        {
            //check all chambers which are running the selected experiment
            for (var i = 0; i < chambersCheckedListBox.Items.Count; i++)
                if (DatabaseProvider.Chambers[chambersCheckedListBox.Items[i].ToString()].RunningExperiment != null &&
                    experimentsComboBox.SelectedItem.ToString() ==
                    DatabaseProvider.Chambers[chambersCheckedListBox.Items[i].ToString()].RunningExperiment.FileName)
                    chambersCheckedListBox.SetItemChecked(i, true);
                else
                    chambersCheckedListBox.SetItemChecked(i, false);
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            foreach (var chamber in DatabaseProvider.Chambers)
                DatabaseProvider.UpdateChamber(chamber.Value);
           
            DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

    }
}
